﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

//CID 0657 //2010.12.09 //Begin
namespace InfoJetSoft.Service.Worker
{
    class EXIDeleteItemWorker
    {
        public static void deleteItem(Form form)
        {
            string contextId = form.ParamTable["xdoc_param_exi_context_id"] as string;
            XmlElement contextElement = DOMUtil.findNodeByJetId(form, contextId) as XmlElement;
            if (contextElement != null)
            {
                contextElement = DOMUtil.selectOneNode(contextElement, "pc:BDCAssociatedEntity", form.XmlNsResolver) as XmlElement;

                //CID 0750 //2010.12.30 //Begin
                string id1 = (form.ParamTable["xdoc_param_exi_entity_id1"] as string).Trim();
                string id2 = (form.ParamTable["xdoc_param_exi_entity_id2"] as string).Trim();
                string id3 = (form.ParamTable["xdoc_param_exi_entity_id3"] as string).Trim();
                string id4 = (form.ParamTable["xdoc_param_exi_entity_id4"] as string).Trim();
                string id5 = (form.ParamTable["xdoc_param_exi_entity_id5"] as string).Trim();

                string entityIdFilter = "pc:BDCEntity/pc:EntityId1";
                ArrayList entityIdList = new ArrayList();
                XmlNodeList entityIdElementList = DOMUtil.selectNodes(contextElement, entityIdFilter, 0, form.XmlNsResolver);
                foreach (XmlElement entityIdElement in entityIdElementList)
                {
                    string curEntityId = entityIdElement.InnerText.Trim();
                    if (curEntityId.Length <= 0)
                    {
                        //CID 0828 //2011.03.10 //Begin
                        if (form.FocusItemIndex == -1)
                        {
                            form.FocusItemIndex = DOMUtil.getChildElementIndex(contextElement, (XmlElement)entityIdElement.ParentNode);
                        }
                        //CID 0828 //2011.03.10 //End
                        contextElement.RemoveChild(entityIdElement.ParentNode);
                    }
                    else
                    {
                        EXIIds entityIds = new EXIIds(form, entityIdElement.ParentNode as XmlElement);
                        if (entityIds.Id1.Equals(id1) &&
                            entityIds.Id2.Equals(id2) &&
                            entityIds.Id3.Equals(id3) &&
                            entityIds.Id4.Equals(id4) &&
                            entityIds.Id5.Equals(id5))
                        {
                            //CID 0828 //2011.03.10 //Begin
                            if (form.FocusItemIndex == -1)
                            {
                                form.FocusItemIndex = DOMUtil.getChildElementIndex(contextElement, (XmlElement)entityIdElement.ParentNode);
                            }
                            //CID 0828 //2011.03.10 //End
                            contextElement.RemoveChild(entityIdElement.ParentNode);
                        }
                    }
                }
                //CID 0750 //2010.12.30 //End
            }
        }
    }
}
//CID 0657 //2010.12.09 //End
